import { nanoid } from "nanoid";
import { cloneDeep } from "lodash-es";
import type { ConfigType } from "./index.d";
import { loadFile } from "./utils/helper";

export const materialModules: Record<string, ConfigType> = import.meta.glob('./components/**/index.ts',{
    eager: true,
    import: "default"
});

export const imageModules: Record<string, string> = import.meta.glob("./images/*.png", {
    eager: true,
    import: "default"
})

export const fetchImage = (name: string) => {
    const images = Object.values(imageModules)
    const imageUrl = images.find((img) => img.includes(name));
    return imageUrl!;
}



export const createComponentConfig = async (config: ConfigType) => {
   const configFile = await loadFile(
        config.category,
        config.package,
        "config.ts"
    );
   // console.log('configFile', configFile);
   return {
    ...cloneDeep(configFile),
    package: config.package,
    category: config.category,
    id: nanoid()
   }
}

export const componentInstall = async (config: ConfigType) => {
    if (window.$vue.component(config.package)) return;
    const file = await loadFile(config.category, config.package, 'index.vue');
    window.$vue.component(config.package, file);
}

export const configComponentInstall = async (config: ConfigType) => {
    const componentName = config.package + "Config";
    if (window.$vue.component(componentName)) return;
    const file = await loadFile(config.category, config.package, 'config.vue');
    window.$vue.component(componentName, file);
}